'use strict';

const Controller = require('egg').Controller;
const jsw=require("jsonwebtoken")
class UserController extends Controller {
  async login() {
        let {ctx}=this
        let {user,pwd}=ctx.query
        if(!user||!pwd){
            ctx.body={
                code:2,
                mes:"没有参数"
            }
            return
        }

        let res=await ctx.service.user.user.login(user,pwd)
        let token=jsw.sign({...res[0]},ctx.app.config.keys,{expiresIn:'10h'})

        console.log(res);
        if(res.length>0){
            ctx.body={
                code:0,
                mes:"登录成功",
                token
            }
        }else{
            ctx.body={
                code:2,
                mes:"登陆失败"
            }
        }
       
  }
  async zc(){
      let {ctx}=this
      let {user,pwd}=ctx.request.body
      console.log(user,pwd);

      if(!user||!pwd){
          ctx.body={
              code:2,
              mes:"没有参数"
          }
      }

      let use=await ctx.service.user.user.zc(user)
      console.log(use);
      if(use.length>0){
          ctx.body={
              code:2,
              mes:"用户名已存在"
          }
          return
      }

      let res=await ctx.service.user.user.add(user,pwd)
      console.log(res);
      if(res.affectedRows==1){
          ctx.body={
              code:0,
              mes:"注册成功"
          }
      }else{
          ctx.body={
              code:2,
              mes:"注册失败"
          }
      }
  }

  
}

module.exports = UserController;


